package org.bluedream.core.module.dao.bill.ord;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.bluedream.core.module.entity.bill.ord.OrdDtl;

import java.io.Serializable;

/**
 * @ClassName OrdDtlDao
 * @Description TODO
 * @Author foxsand
 * @Data 14:42
 * @Version
 */
public interface OrdDtlDao extends BaseMapper<OrdDtl> {

    @Select("select ordDtl.bill_type as billType , ordDtl.bill_num as billNum , ordDtl.code as code, depItemUser.barcode as barcode, depItemUser.name as name, ordDtl.qty as qty ," +
            " ordDtl.id as id , ordDtl.create_by as createBy , ordDtl.create_date as createDate , ordDtl.update_by as updateBy , ordDtl.update_date as updateDate , ordDtl.status as status , ordDtl.remarks as remarks , " +
            "   case " +
            "       when mod(ordDtl.qty , depItemUser.qpc) = 0 then to_char(ordDtl.qty / depItemUser.qpc)  " +
            "       when mod(ordDtl.qty , depItemUser.qpc) != 0  and ordDtl.qty/depItemUser.qpc = 0 then to_char(floor(ordDtl.qty/depItemUser.qpc))||'+'||to_char(ordDtl.qty) " +
            "       when mod(ordDtl.qty , depItemUser.qpc) != 0  and ordDtl.qty/depItemUser.qpc != 0 then to_char(floor(ordDtl.qty/depItemUser.qpc))||'+'||to_char(ordDtl.qty - depItemUser.qpc) end as qtyStr , " +
            "depItemUser.qpc as qpc , depItemUser.qpc_str as qpcStr , depItemUser.unit as unit , depItemUser.spec as spec , ordDtl.oprice as oprice , ordDtl.total as total " +
            "from OrdDtl ordDtl , depItemUser depItemUser " +
            " ${ew.customSqlSegment} and ordDtl.code = depItemUser.code ")
    IPage<OrdDtl> selectPage(IPage<OrdDtl> page, @Param("ew") Wrapper<OrdDtl> wrapper);

    @Override
    @Select("select ordDtl.bill_type as billType , ordDtl.bill_num as billNum , ordDtl.code as code, depItemUser.barcode as barcode, depItemUser.name as name, ordDtl.qty as qty ," +
            " ordDtl.id as id , ordDtl.create_by as createBy , ordDtl.create_date as createDate , ordDtl.update_by as updateBy , ordDtl.update_date as updateDate , ordDtl.status as status , ordDtl.remarks as remarks , " +
            "   case " +
            "       when mod(ordDtl.qty , depItemUser.qpc) = 0 then to_char(ordDtl.qty / depItemUser.qpc)  " +
            "       when mod(ordDtl.qty , depItemUser.qpc) != 0  and ordDtl.qty/depItemUser.qpc = 0 then to_char(floor(ordDtl.qty/depItemUser.qpc))||'+'||to_char(ordDtl.qty) " +
            "       when mod(ordDtl.qty , depItemUser.qpc) != 0  and ordDtl.qty/depItemUser.qpc != 0 then to_char(floor(ordDtl.qty/depItemUser.qpc))||'+'||to_char(ordDtl.qty - depItemUser.qpc) end as qtyStr , " +
            "depItemUser.qpc as qpc , depItemUser.qpc_str as qpcStr , depItemUser.unit as unit , depItemUser.spec as spec , ordDtl.oprice as oprice , ordDtl.total as total " +
            "from OrdDtl ordDtl , depItemUser depItemUser " +
            " where ordDtl.id = #{id} and ordDtl.code = depItemUser.code ")
    OrdDtl selectById(Serializable id);
}
